package com.zenithmind.chat.pojo.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * AI对话统计信息VO
 * 遵循单一职责原则，专门用于展示统计数据
 */
@Data
@Schema(description = "AI对话统计信息VO")
public class AiChatStatisticsVO {
    
    @Schema(description = "总消息数")
    private Integer totalMessages;
    
    @Schema(description = "用户消息数")
    private Integer userMessages;
    
    @Schema(description = "助手消息数")
    private Integer assistantMessages;
    
    @Schema(description = "总token数")
    private Integer totalTokens;
    
    @Schema(description = "用户token数")
    private Integer userTokens;
    
    @Schema(description = "助手token数")
    private Integer assistantTokens;
    
    @Schema(description = "平均响应时间(毫秒)")
    private Long averageResponseTime;
    
    @Schema(description = "最快响应时间(毫秒)")
    private Long fastestResponseTime;
    
    @Schema(description = "最慢响应时间(毫秒)")
    private Long slowestResponseTime;
    
    @Schema(description = "成功率(%)")
    private Double successRate;
    
    @Schema(description = "失败消息数")
    private Integer failedMessages;
    
    @Schema(description = "第一条消息时间")
    private LocalDateTime firstMessageTime;
    
    @Schema(description = "最后一条消息时间")
    private LocalDateTime lastMessageTime;
    
    @Schema(description = "活跃天数")
    private Integer activeDays;
    
    @Schema(description = "平均每天消息数")
    private Double averageMessagesPerDay;
    
    /**
     * 获取格式化的成功率
     */
    public String getFormattedSuccessRate() {
        if (successRate == null) {
            return "0%";
        }
        return String.format("%.1f%%", successRate);
    }
    
    /**
     * 获取格式化的平均响应时间
     */
    public String getFormattedAverageResponseTime() {
        if (averageResponseTime == null || averageResponseTime == 0) {
            return "0ms";
        }
        
        if (averageResponseTime >= 60000) {
            return String.format("%.1f分钟", averageResponseTime / 60000.0);
        } else if (averageResponseTime >= 1000) {
            return String.format("%.1f秒", averageResponseTime / 1000.0);
        }
        
        return averageResponseTime + "ms";
    }
    
    /**
     * 获取格式化的总token数
     */
    public String getFormattedTotalTokens() {
        if (totalTokens == null || totalTokens == 0) {
            return "0";
        }
        
        if (totalTokens >= 1000000) {
            return String.format("%.1fM", totalTokens / 1000000.0);
        } else if (totalTokens >= 1000) {
            return String.format("%.1fK", totalTokens / 1000.0);
        }
        
        return totalTokens.toString();
    }
    
    /**
     * 获取token使用效率描述
     */
    public String getTokenEfficiencyDescription() {
        if (totalMessages == null || totalMessages == 0 || totalTokens == null || totalTokens == 0) {
            return "无数据";
        }
        
        double avgTokensPerMessage = (double) totalTokens / totalMessages;
        
        if (avgTokensPerMessage < 50) {
            return "高效";
        } else if (avgTokensPerMessage < 200) {
            return "正常";
        } else if (avgTokensPerMessage < 500) {
            return "较高";
        } else {
            return "很高";
        }
    }
    
    /**
     * 获取活跃度描述
     */
    public String getActivityDescription() {
        if (averageMessagesPerDay == null || averageMessagesPerDay == 0) {
            return "不活跃";
        }
        
        if (averageMessagesPerDay >= 20) {
            return "非常活跃";
        } else if (averageMessagesPerDay >= 10) {
            return "活跃";
        } else if (averageMessagesPerDay >= 5) {
            return "一般";
        } else {
            return "较少";
        }
    }
}
